package com.shujia

import com.shujia.Demo21Match.Week

object Demo21Match {
  def main(args: Array[String]): Unit = {
    /**
     * Scala中的模式匹配，相当于Java中的switch操作
     * 通过match可以进行匹配：
     * 1、基本数据类型（值类型）
     * 2、字符串
     * 3、枚举类
     * 4、类型
     * 5、样例类对象
     */

    // 1、基本数据类型（值类型）
    val i: Int = 10
    i match {
      case 10 =>
        println("i的值为10")
      case 20 =>
        println("i的值为20")
      case _ =>
        println("其他情况")

    }

    // 2、字符串
    val str1: String = "C"
    str1 match {
      case "Java" =>
        println("Hello Java")
      case "Scala" =>
        println("Hello Scala")
      case "Python" =>
        println("Hello Python")
      case _ => // 相当于Java中的default
        println("Other")
    }

    // Scala中的模式匹配是支持有返回值的
    val str2: String = "C"
    val matchRes: String = str2 match {
      case "Java" =>
        "Hello Java !"
      case "Scala" =>
        "Hello Scala !"
      case "Python" =>
        "Hello Python !"
      case _ => // 相当于Java中的default
        "Other !"
    }

    println(matchRes)

    // 3、枚举类
    val wednesday: Week.Value = Week.Wednesday
    wednesday match {
      case Week.Monday =>
        println("上课")
      case Week.Tuesday =>
        println("上课")
      case Week.Wednesday =>
        println("自习")
      case Week.Thursday =>
        println("上课")
      case Week.Friday =>
        println("上课")
      case Week.Saturday =>
        println("上课")
      case Week.Sunday =>
        println("放假")
    }

    // 4、类型
    val value: Any = 100

    value match {
      case v: Int =>
        println(s"${v}是Int类型")
      case v: String =>
        println(s"${v}是String类型")
      case _ =>
        println("value是其他类型")
    }

    // 5、样例类对象
    case class StuMatch(id: String, name: String, age: Int)

    val ls: StuMatch = StuMatch("002", "ls", 20)

    ls match {
      case StuMatch("001", "ls", 20) =>
        println("ls 001")
      case StuMatch("002", "ls", 20) =>
        println("ls 002")
      case StuMatch("001", "ls", 22) =>
        println("ls age 22")
    }

    // 模式匹配在对数据容器使用map操作时，可以简化调用
    val complexMap: Map[String, (String, (Int, (String, String)))] = Map(
      ("001", ("zs", (20, ("男", "文科一班"))))
      , ("002", ("ls", (21, ("女", "文科四班"))))
      , ("003", ("ww", (22, ("男", "理科一班"))))
    )

    val complexList: List[(String, (String, (Int, (String, String))))] = List(
      ("001", ("zs", (20, ("男", "文科一班"))))
      , ("002", ("ls", (21, ("女", "文科四班"))))
      , ("003", ("ww", (22, ("男", "理科一班"))))
    )

    // 最终需要将Map中的每一条数据中的每一个值拼接在一起 转换成 普通的字符串
    complexMap.map(kv => {
      val id: String = kv._1
      val name: String = kv._2._1
      val age: Int = kv._2._2._1
      val gender: String = kv._2._2._2._1
      val clazz: String = kv._2._2._2._2
      s"$id,$name,$age,$gender,$clazz"
    }).foreach(println)

    // 使用模式匹配进行简化
    complexMap.map{
      case (id:String, (name:String, (age:Int, (gender:String, clazz:String)))) =>
        s"$id,$name,$age,$gender,$clazz"
    }.foreach(println)

    complexList.map{
      case (id:String, (name:String, (age:Int, (gender:String, clazz:String)))) =>
        s"$id,$name,$age,$gender,$clazz"
    }.foreach(println)

  }

  // 在Scala中定义一个枚举类型
  object Week extends Enumeration {
    val Monday: Week.Value = Value("Mon.")
    val Tuesday: Week.Value = Value("Tue.")
    val Wednesday: Week.Value = Value("Wed.")
    val Thursday: Week.Value = Value("Thur.")
    val Friday: Week.Value = Value("Fri.")
    val Saturday: Week.Value = Value("Sat.")
    val Sunday: Week.Value = Value("Sun.")
  }

}

